package com.lx.Cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/cookieDemo1")
public class CookieDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//1. 创建cookie对象
        Cookie c = new Cookie("msg", "cookie");
        //设置cookie存活时间
        c.setMaxAge(60);  //将cookie持久化到硬盘 60s后自动删除cookie文件（默认负数 0为删除cookie）
        //2. 发送cookie
        response.addCookie(c);
/*
    c.setPath("/");       设置cookie共享 默认为当前的虚拟目录 如果要共享则设置为 "/"
    c.setDomain(".baidu.com");  不同服务器的共享 设置一级域名相同
 */
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
